package com.ellricky.baselib.ui.order

import android.os.Bundle
import android.view.View
import androidx.recyclerview.widget.RecyclerView
import com.andr.common.ui.fragment.BaseFragment
import com.andr.common.ui.mvp.BasePresenter
import com.andr.common.widget.refresh.RvRefreshListener
import com.ellricky.baselib.BaseConstant
import com.ellricky.baselib.R
import com.ellricky.baselib.cache.LocalCacheUtil
import com.ellricky.baselib.dialog.picker.EPickerUtil
import com.ellricky.baselib.entity.VisitListInfo
import com.ellricky.baselib.http.HttpUtils
import com.ellricky.baselib.util.EAppUtil
import com.ellricky.baselib.util.NewEBaseCallback
import com.ellricky.baselib.util.NewEListCallback
import com.ellricky.baselib.util.NewEntityCallback
import kotlinx.android.synthetic.main.base_layout_visit_list.*
import java.lang.Exception

class VisitListFragment : BaseFragment<Void, BasePresenter<Void>>(), RvRefreshListener {

    private var paramMap: HashMap<String, String> = hashMapOf()
    private var dataSource: List<VisitListInfo> = arrayListOf()
    private var ePickerUtil: EPickerUtil? = null
    private var refresh = false

    private val memberId by lazy {
        arguments?.getString(BaseConstant.KEY_ID, "") ?: ""
    }
    private val adapter by lazy {
        VisitListAdapter()
    }

    companion object {
        fun newInstance(bundle: Bundle?): VisitListFragment {
            val fragment = VisitListFragment()
            fragment.arguments = bundle
            return fragment
        }
    }

    override fun getContentLayout(): Int {
        return R.layout.base_layout_visit_list
    }

    override fun initUIView(p0: View?) {
        tv_title.text = "回访列表"

        rvView.init(this)

        ePickerUtil = EPickerUtil(requireActivity())

        initListener()

        loadDataOnInit()
    }

    private fun initListener() {
        ib_back.setOnClickListener {
            activity?.finish()
        }
    }

    private fun initParamMap() {
        paramMap["action"] = "meberReturnList"
        paramMap["eid"] = LocalCacheUtil.getUid()
        paramMap["member_id"] = memberId
        paramMap["page"] = rvView.getCurPage().toString()
        paramMap["page_size"] = "40"
    }

    private fun loadDataOnInit() {
        rvView.loadContent(true)
    }

    override fun setAdapter(rv: RecyclerView?) {
        rv?.adapter = adapter
    }

    override fun loadData(refresh: Boolean) {
        try {
            this.refresh = refresh
            val url = EAppUtil.getApiHost()
            initParamMap()
            val callback: NewEBaseCallback = getResponseCallback2()
            HttpUtils.getInstance().postFrom(url, paramMap, callback)
        }catch (e:Exception){
            e.printStackTrace()
        }
    }

    private fun getResponseCallback2(): NewEBaseCallback {
        return object : NewEListCallback<VisitListInfo>(requireActivity()) {
            override fun onSuccess(data: List<VisitListInfo>, msg: String?) {
                dataSource = data
                rvView.onLoadFinish(data, refresh)
            }
        }
    }

    override fun updateAdapterData(refresh: Boolean) {
        if (refresh) adapter.refresh(dataSource)
        else adapter.loadMore(dataSource)
    }
}